package com.burstly.lib.apptracking;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import com.burstly.lib.feature.apptracker.IAppTracker;
import com.burstly.lib.feature.apptracker.IAppTrackingListener;
import com.burstly.lib.util.LoggerExt;
import com.burstly.lib.util.PrioritizedAsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public final class AppTrackingManager
  implements IAppTracker
{
  private static final LoggerExt LOG = LoggerExt.getInstance();
  private static final String SERVICE_URL = "http://an.appads.com/Info.svc/UrlSchemeGet/";
  private static final String TAG = "InstalledAppTracker";

  private static Map<String, AppInstallState> checkInstalledApps(Context paramContext, List<String> paramList)
  {
    List localList = paramContext.getPackageManager().getInstalledApplications(0);
    LOG.logDebug("InstalledAppTracker", "All installed apps: {0}", new Object[] { localList });
    HashMap localHashMap = new HashMap((int)(paramList.size() / 0.75D));
    Iterator localIterator1 = paramList.iterator();
    while (localIterator1.hasNext())
      localHashMap.put((String)localIterator1.next(), AppInstallState.NOT_INSTALLED);
    Iterator localIterator2 = localList.iterator();
    while (localIterator2.hasNext())
    {
      ApplicationInfo localApplicationInfo = (ApplicationInfo)localIterator2.next();
      if (localHashMap.get(localApplicationInfo.packageName) != null)
        localHashMap.put(localApplicationInfo.packageName, AppInstallState.INSTALLED);
    }
    LOG.logDebug("InstalledAppTracker", "Detected app states: {0}", new Object[] { localHashMap });
    return localHashMap;
  }

  private static String createPubTargetingString(Map<String, AppInstallState> paramMap)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    Iterator localIterator = paramMap.entrySet().iterator();
    while (localIterator.hasNext())
    {
      Map.Entry localEntry = (Map.Entry)localIterator.next();
      localStringBuilder.append((String)localEntry.getKey());
      localStringBuilder.append('=');
      localStringBuilder.append(((AppInstallState)localEntry.getValue()).getStateValue());
      if (localIterator.hasNext())
        localStringBuilder.append(", ");
    }
    LOG.logDebug("InstalledAppTracker", "Constructed pub targeting string: {0}", new Object[] { localStringBuilder });
    return localStringBuilder.toString();
  }

  private static List<String> downloadPackageNames(String paramString)
  {
    try
    {
      LOG.logDebug("InstalledAppTracker", "Downloading package names from server... Service url: {0}. Organization ID: {1}", new Object[] { "http://an.appads.com/Info.svc/UrlSchemeGet/", paramString });
      localBufferedReader = new BufferedReader(new InputStreamReader(new URL("http://an.appads.com/Info.svc/UrlSchemeGet/" + paramString).openStream()));
      localStringBuilder = new StringBuilder();
      while (true)
      {
        String str1 = localBufferedReader.readLine();
        if (str1 == null)
          break;
        localStringBuilder.append(str1);
      }
    }
    catch (MalformedURLException localMalformedURLException)
    {
      BufferedReader localBufferedReader;
      StringBuilder localStringBuilder;
      LOG.logThrowable("InstalledAppTracker", localMalformedURLException);
      return null;
      localBufferedReader.close();
      String str2 = localStringBuilder.toString();
      LOG.logDebug("InstalledAppTracker", "Received data from server: {0}", new Object[] { str2 });
      List localList = parsePackageNames(str2);
      return localList;
    }
    catch (IOException localIOException)
    {
      while (true)
        LOG.logThrowable("InstalledAppTracker", localIOException);
    }
  }

  private static String getPubTargetingString(Context paramContext, String paramString)
  {
    if (paramContext == null)
      LOG.logDebug("InstalledAppTracker", "Context is null. Cannot get installed apps.", new Object[0]);
    List localList;
    do
    {
      while (true)
      {
        return "";
        if ((paramString != null) && (paramString.length() != 0))
          break;
        LOG.logDebug("InstalledAppTracker", "Organization ID is null or empty. Cannot get package names from server.", new Object[0]);
      }
      localList = downloadPackageNames(paramString);
    }
    while ((localList == null) || (localList.size() <= 0));
    return createPubTargetingString(checkInstalledApps(paramContext, localList));
  }

  private static List<String> parsePackageNames(String paramString)
  {
    String[] arrayOfString = paramString.split("[ ,;\\[\\]\t\r\n\"']");
    ArrayList localArrayList = new ArrayList();
    int i = arrayOfString.length;
    for (int j = 0; j < i; j++)
    {
      String str = arrayOfString[j];
      if (str.length() != 0)
        localArrayList.add(str);
    }
    LOG.logDebug("InstalledAppTracker", "Splitted apps: {0}. Will check their installation state...", new Object[] { localArrayList });
    return localArrayList;
  }

  public AsyncTask<Void, Void, Void> getPubTargetingStringAsync(final Context paramContext, final String paramString, final IAppTrackingListener paramIAppTrackingListener)
  {
    LOG.logDebug("InstalledAppTracker", "Starting new app tracking task...", new Object[0]);
    if (paramIAppTrackingListener == null)
    {
      LOG.logDebug("InstalledAppTracker", "Listener is null. No way to publish results.", new Object[0]);
      return null;
    }
    return new PrioritizedAsyncTask()
    {
      protected Void performInBackground()
      {
        try
        {
          paramIAppTrackingListener.getPubTargetingStringComplete(AppTrackingManager.getPubTargetingString(paramContext, paramString));
          return null;
        }
        catch (Exception localException)
        {
          while (true)
            AppTrackingManager.LOG.logThrowable("InstalledAppTracker", localException);
        }
      }
    }
    .execute(new Void[0]);
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.apptracking.AppTrackingManager
 * JD-Core Version:    0.6.2
 */